package com.zj.leetcode.medium.listNode;

/**
 * @program: algorithm
 * @description: 两数相加
 * 2 add-two-numbers
 * @author: Zhang Bo
 * @create: 2022-01-14 17:05
 **/
public class AddTwoNumbers {
    public static void main(String[] args) {
        ListNode l1 = new ListNode(5,
                new ListNode(8, null));
        ListNode l2 = new ListNode(7,
                new ListNode(4, new ListNode(1, null)));

        Solution solution = new AddTwoNumbers().new Solution();
        System.out.println(solution.addTwoNumbers(new ListNode(0), new ListNode(0)));
    }
    //leetcode submit region begin(Prohibit modification and deletion)

    /**
     * Definition for singly-linked list.
     * public class ListNode {
     * int val;
     * ListNode next;
     * ListNode() {}
     * ListNode(int val) { this.val = val; }
     * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
     * }
     */

    public static class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }

        @Override
        public String toString() {
            return "ListNode{" +
                    "val=" + val +
                    ", next=" + next +
                    '}';
        }
    }

    class Solution {

        public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
            if (l1 == null && l2 == null) {
                return null;
            }
            if (l1 == null) {
                return l2;
            }
            if (l2 == null) {
                return l1;
            }

            ListNode ret = new ListNode(0);
            ListNode cur = ret;
            int bitCarry = 0;
            while (l1 != null || l2 != null) {
                int sum = 0;
                if (l1 != null && l2 != null) {
                    sum = l1.val + l2.val + bitCarry;
                } else if (l1 == null) {
                    sum = l2.val + bitCarry;
                } else if (l2 == null) {
                    sum = l1.val + bitCarry;
                }

                int var = 0;
                if (sum >= 10) {
                    var = sum % 10;
                    bitCarry = sum / 10;
                } else {
                    var = sum;
                    bitCarry = 0;
                }
                cur.next = new ListNode(var, null);
                if (l1 != null) {
                    l1 = l1.next;
                }
                if (l2 != null) {
                    l2 = l2.next;
                }
                cur = cur.next;
            }
            if(bitCarry != 0){
                cur.next = new ListNode(bitCarry, null);
            }

            if (ret.val == 0) {
                ret = ret.next;
            }

            return ret;

        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}
